package com.newsee.remote.ecommerce.service.impl;

import com.newsee.remote.common.bean.JsonResult;
import com.newsee.remote.common.bean.JsonReturn;
import com.newsee.remote.common.enumeration.EcommerceEnum;
import com.newsee.remote.common.exception.ServiceException;
import com.newsee.remote.common.utils.ecache.ConcurrentHashMapCacheUtil;
import com.newsee.remote.ecommerce.commerce.colipu.service.IColipuCategoryService;
import com.newsee.remote.ecommerce.service.ICommerceService;
import org.springframework.stereotype.Service;

/**
 * @author liangzhi.chen
 * @description 电商平台公共impl
 * @date 2019/7/16 下午 12:00
 **/
@Service
public class ICommerceServiceImpl implements ICommerceService {

    private final IColipuCategoryService IColipuCategoryService;

    /**
     * 1个小时
     */
    private static final long CACHE_HOLD_TIME_HALF_HOUR = 60 * 60 * 1000L;

    public ICommerceServiceImpl(IColipuCategoryService icolipucategoryservice) {
        this.IColipuCategoryService = icolipucategoryservice;
    }

    @Override
    public JsonResult sync() {
        Object o = ConcurrentHashMapCacheUtil.get(EcommerceEnum.E_COLIPU.getDesc() + "sync");
        if (null != o) {
            throw new ServiceException("科力普商品同步接口正在同步中！");
        }
        //添加锁
        ConcurrentHashMapCacheUtil.put(EcommerceEnum.E_COLIPU.getDesc() + "sync", EcommerceEnum.E_COLIPU.getDesc() + "sync", CACHE_HOLD_TIME_HALF_HOUR);

        //1.开启科力普同步接口(异步)
        IColipuCategoryService.sync();
        return JsonReturn.succ("科力普商品同步开始...");
    }

}
